package com.UI
{
	import com.*;
	import com.Engine.*;
	import com.Packet.*;
	
	import flash.display.*;
	import flash.events.*;
	import flash.external.*;
	import flash.filters.*;
	import flash.geom.*;
	import flash.system.*;
	import flash.text.*;
	import flash.ui.*;
	import flash.utils.*;
	
	import org.aswing.*;
	import org.aswing.border.*;
	import org.aswing.border.EmptyBorder;
	import org.aswing.colorchooser.*;
	import org.aswing.dnd.*;
	import org.aswing.event.*;
	import org.aswing.ext.*;
	import org.aswing.geom.*;
	import org.aswing.tree.AbstractLayoutCache;
	
	public class FrameLogin extends JFrame
	{
		/**
		 * 表单容器
		 */ 
		//members define
		private var _txtName:JTextField;
		private var _txtPassword:JTextField;
		private var _labelMessage:JLabel;
		private var panel71:JPanel;
		private var _btn_register:JButton;
		private var _btn_login:JButton;
		//private var _btn_cancel:JButton = new JButton();//'取消'
		public function FrameLogin()
		{
			super();
			setClosable(false);
			setTitleBar(null);
			setResizable(false);
			setBackgroundDecorator(null);
			setBorder(new EmptyBorder());
			
			//component creation
			setSize(new IntDimension(240, 163));
			setPreferredSize(new IntDimension(240, 163));
			var layout0:EmptyLayout = new EmptyLayout();
			getContentPane().setLayout(layout0);
			
			_txtName = new JTextField();
			_txtName.setLocation(new IntPoint(70, 31));
			_txtName.setSize(new IntDimension(137, 24));
			_txtName.setPreferredSize(new IntDimension(137, 24));
			
			_txtPassword = new JTextField();
			_txtPassword.setLocation(new IntPoint(70, 63));
			_txtPassword.setSize(new IntDimension(137, 21));
			_txtPassword.setPreferredSize(new IntDimension(137, 24));
			
			_labelMessage = new JLabel();
			_labelMessage.setLocation(new IntPoint(0, 92));
			_labelMessage.setSize(new IntDimension(240, 21));
			_labelMessage.setPreferredSize(new IntDimension(240, 21));
			_labelMessage.setText("label");
			
			panel71 = new JPanel();
			panel71.setLocation(new IntPoint(0, 115));
			panel71.setSize(new IntDimension(240, 25));
			panel71.setPreferredSize(new IntDimension(240, 25));
			var layout1:FlowWrapLayout = new FlowWrapLayout();
			layout1.setPreferWidth(200);
			layout1.setAlignment(AsWingConstants.CENTER);
			layout1.setHgap(20);
			layout1.setVgap(0);
			panel71.setLayout(layout1);
			
			_btn_register = new JButton();
			_btn_register.setLocation(new IntPoint(5, 5));
			_btn_register.setSize(new IntDimension(31, 23));
			_btn_register.setPreferredSize(new IntDimension(57, 25));
			_btn_register.setText("label");
			
			_btn_login = new JButton();
			_btn_login.setLocation(new IntPoint(122, 0));
			_btn_login.setSize(new IntDimension(57, 25));
			_btn_login.setPreferredSize(new IntDimension(57, 25));
			_btn_login.setText("label");
			
			//component layoution
			getContentPane().append(_txtName);
			getContentPane().append(_txtPassword);
			getContentPane().append(_labelMessage);
			getContentPane().append(panel71);
			
			panel71.append(_btn_register);
			panel71.append(_btn_login);
			
			
			//var layout:SoftBoxLayout = new SoftBoxLayout(SoftBoxLayout.TOP,0,0);
			//layout.setAxis(BoxLayout.Y_AXIS)
			//getContentPane().setLayout(layout);
			//setBorder(new EmptyBorder());
			
			// 增加循环背景控制
			//_txtName = new JTextField('',12);
			_txtName.setBackgroundDecorator(null);
			_txtName.setMaxChars(16);
			_txtName.setRestrict('0-9,a-z,A-Z,_');

			//_txtPassword = new JTextField('',12);
			_txtPassword.setBackgroundDecorator(null);
			_txtPassword.setDisplayAsPassword(true);
			_txtPassword.setMaxChars(16);
			
			var txtFormat:TextFormat = new TextFormat("Courier New", 14, 0xFFFFFF, false, false, false);
			
			_txtName.setDefaultTextFormat(txtFormat);
			_txtPassword.setDefaultTextFormat(txtFormat);
			
			_txtName.addActionListener(onLogin);
			_txtPassword.addActionListener(onLogin);
			
			var filter:GlowFilter;
			filter = new GlowFilter(0x000000, 1, 2, 2, 100);
			_txtName.filters = new Array(filter);
			_txtPassword.filters = new Array(filter);
			
			//_labelMessage = new JLabel('',null,JLabel.LEFT);
			_labelMessage.setFont(new ASFont("宋体", 14, false, false, false, false));
			filter = new GlowFilter(0x000000, 1, 2, 2, 10);
			_labelMessage.setTextFilters(new Array(filter));
			_labelMessage.setForeground( ASColor.RED ); 
			_labelMessage.setText("");
			
			//_btn_register = new JButton();//'注册'
			//_btn_login = new JButton();//'登陆'
			//_btn_cancel = new JButton();//'取消'
			
			_btn_register.addActionListener(onRegister);
			_btn_login.addActionListener(onLogin);
			//_btn_cancel.addActionListener(onCancel);
			
			_btn_register.wrapSimpleButton(Client.getUIElement("btn_register") as SimpleButton);
			_btn_login.wrapSimpleButton(Client.getUIElement("btn_login") as SimpleButton);
			//_btn_cancel.wrapSimpleButton(Client.getUIElement("btn_cancel") as SimpleButton);
			_btn_register.setText("");
			_btn_login.setText("");
			
			var img_login:DisplayObject = Client.getUIElement("img_login") as DisplayObject;
			setBackgroundDecorator(new AssetBackground(img_login));
			
			/*var p:JPanel= new JPanel(new SoftBoxLayout(SoftBoxLayout.BOTTOM,0,0));
			p.setPreferredHeight(10);
			getContentPane().append(p);
			
			p= new JPanel(new SoftBoxLayout(SoftBoxLayout.BOTTOM,0,0));
			var pEmpty:JPanel = new JPanel();
			pEmpty.setPreferredWidth(27);
			p.setPreferredHeight(28);
			p.appendAll(pEmpty,_txtName);
			getContentPane().append(p);
			
			p = new JPanel(new SoftBoxLayout(SoftBoxLayout.BOTTOM,0,0));
			pEmpty = new JPanel();
			pEmpty.setPreferredWidth(27);
			p.setPreferredHeight(20);
			p.appendAll(pEmpty,_txtPassword);
			getContentPane().append(p);
			
			p = new JPanel(new SoftBoxLayout(SoftBoxLayout.BOTTOM,0,0));
			p.setPreferredHeight(14);
			p.appendAll(_labelMessage);
			getContentPane().append(p);
			
			p = new JPanel(new FlowLayout(FlowLayout.CENTER,16,0));
			p.appendAll(_btn_register,_btn_login);
			p.setPreferredHeight(32);
			getContentPane().append(p);*/
			
			setSizeWH(img_login.width,img_login.height);
			setLocationXY((Engine.W - img_login.width) / 2,(Engine.H - img_login.height)/2);
			
			//var param:Object = stage.loaderInfo.parameters;
			if (Engine.open_id.length != 0 && Engine.open_key.length != 0) 
			{
				_txtName.setText(Engine.open_id);
				_txtPassword.setText(Engine.open_key);
				bQQAccount = true;
				onLogin(null);
			} 
			
			/*bQQAccount = true;
			_txtName.setText("tt");
			_txtPassword.setText("tt");
			onLogin(null);*/

			_txtName.setEnabled(!bQQAccount);
			_txtPassword.setEnabled(!bQQAccount);
			_btn_register.setEnabled(!bQQAccount);
			_btn_login.setEnabled(!bQQAccount);
			//_btn_cancel.setEnabled(!bQQAccount);
			
			_txtName.setVisible(!bQQAccount);
			_txtPassword.setVisible(!bQQAccount);
			_btn_register.setVisible(!bQQAccount);
			_btn_login.setVisible(!bQQAccount);
			//_btn_cancel.setVisible(!bQQAccount);
			
			if(bQQAccount)
			{
				setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_info_bg") as DisplayObject));
				_labelMessage.setFont(new ASFont("宋体", 14, true, false, false, false));
				filter = new GlowFilter(0xffffff, 1, 2, 2, 10);
				_labelMessage.setTextFilters(new Array(filter));
				_labelMessage.setForeground( new ASColor(Engine.colorSystem)); 
				_labelMessage.setClipMasked(false);
				setClipMasked(false);
				getContentPane().setClipMasked(false);
			}
			Client.getInstance().addEventListener("1", phLogin_1);
			//addEventListener(FrameEvent.FRAME_CLOSING,onFrameClosing);
		}
		private function onFrameClosing(e:Event):void
		{
			_labelMessage.setText("");
		}
		private var bQQAccount:Boolean = false;
		static private var bLogining:Boolean = false;
		private function showMessage(msg:String):void
		{
			_labelMessage.setText(msg);
			validate();
		}

		protected function onRegister(e:Event):void
		{
			FrameRegister.show();
		}
		protected function onLogin(e:Event):void
		{
			if(_txtName.getText().length <= 0)
			{
				showMessage("请输入用户名!");
				return;
			}
			if(_txtPassword.getText().length <= 0)
			{
				showMessage("请输入密码!");
				return;
			}
			
			_txtName.setEnabled(false);
			_txtPassword.setEnabled(false);
			_btn_register.setEnabled(!bQQAccount);
			_btn_login.setEnabled(!bQQAccount);
			//_btn_cancel.setEnabled(!bQQAccount);

			if(!Client.getInstance().beConnected)
			{
				if(!Client.getInstance().beConnecting)
					Client.getInstance().connect();
				showMessage("正在连接服务器...");
				Client.getInstance().socket.addEventListener(Event.CONNECT, handleSocketConnect);
			}
			else
			{
				if(!bLogining)
				{
					showMessage("正在登陆...");
					bLogining = true;
					Client.getInstance().sendPacketLogic(0);
					Client.getInstance().sendPacketLogic(1,[Engine.account_type,_txtName.getText(),_txtPassword.getText()]);
				}
			}
		}
		
		private function handleSocketConnect(e:Event):void
		{
			Client.getInstance().socket.removeEventListener(Event.CONNECT, handleSocketConnect);
			if(!bLogining)
			{
				showMessage("连接服务器成功,正在登陆...")
				Client.getInstance().sendPacketLogic(1,[Engine.account_type,_txtName.getText(),_txtPassword.getText()]);
				bLogining = true;
			}
		}
		public function resetUI():void
		{
			_txtName.setEnabled(true);
			_txtPassword.setEnabled(true);
			_btn_register.setEnabled(true);
			_btn_login.setEnabled(true);
			_labelMessage.setText("");
		}
		
		public function phLogin_1(e:PacketEvent):void
		{
			var msg:Array = e.data as Array;
			trace("phLogin_1", msg);
			switch(msg[0])
			{
				case 0:
				{
					showMessage("登陆成功,正在加载玩家信息...");
					//_panelMain.createCharacter();
					//closeReleased();
					CharInfo.accountId = uint(msg[1]);
					return;
					//break;
				}
				case 1:
				{
					showMessage("此帐号不存在!");
					break;
				}
				case 2:
				{
					showMessage("密码错误!");
					break;
				}
				case 3:
				{
					showMessage("此帐号已经登陆!");
					break;
				}
				case 4:
				{
					showMessage("数据库操作出错!");
					break;
				}
				case 5:
				{
					showMessage("服务器人数已达上限!");
					break;
				}
			}
			bLogining = false;
			_txtName.setEnabled(!bQQAccount);
			_txtPassword.setEnabled(!bQQAccount);
			_btn_register.setEnabled(!bQQAccount);
			_btn_login.setEnabled(!bQQAccount);
			//_btn_cancel.setEnabled(!bQQAccount);
		}
		
		static public function phLogout_2(e:PacketEvent):void
		{
			//Client.getInstance().removeEventListener("1", phLogin_1);
			
			var msg:Array = e.data as Array;
			trace("phLogout_2", msg);
			FrameLogin.show();
			FrameInfo.showMessageDialog("","已经从别处登陆,请重新登陆!");
			if(PanelMainHelp.getInstance().contains(SceneMain.getInstance()))
				PanelMainHelp.getInstance().removeChild(SceneMain.getInstance());
			if(PanelMain.getInstance().isInBattle)
				PanelMain.getInstance().leaveBattle();
			if(PanelMainHelp.getInstance().contains(SceneBattle.getInstance()))
				PanelMainHelp.getInstance().removeChild(SceneBattle.getInstance());
			CharInfo.clear();
			SceneMain.getInstance().clearup();
			Client.getInstance().disconnect();
		}
		
		protected function onCancel(e:Event):void
		{
			FrameInfo.showMessageDialog("","谢谢光顾!,请关闭浏览器退出游戏!");
		}
		
		public static var _frame:FrameLogin = null;
		public static function show():void
		{
			FrameRegister.hide();
			FrameCreate.hide();
			
			if(isShowing())
			{
				//hide();
				return;
			}
			if(_frame == null)
			{
				_frame = new FrameLogin();
				PanelMain.getInstance().addChild(_frame);
			}
			_frame.show();
			bLogining = false;
			PanelMain.getInstance().setBackgroundDecorator(null);
			PanelMain.getInstance().setBackgroundDecorator(new AssetBackground(Client.getUIElement("img_create_background") as DisplayObject));
			PanelMain.getInstance().showPanelMainHelp(false);
			PanelMain.getInstance().bringToTop(_frame);
			
		}
		public static function hide():void
		{
			if(isShowing())_frame.hide();
			_frame.resetUI();
		}
		public static function isShowing():Boolean
		{
			return(_frame != null&& _frame.isShowing());
		}
	}
}